#include <fs.h>
#include <fs/devfs.h>
#include <fs/ramfs.h>
#include "fat/fat.h"
#include <mm/valloc.h>

static lnode_t fs_list = { .next = &fs_list, .prev = &fs_list };

void fs_install_default() {
    ramfs_init();
    devfs_init();
    fat32_init();
}

fs_t* fs_alloc_fs(char* name, u32 name_len) {
    fs_t* fs = valloc(sizeof(fs_t));
    memset(fs, 0, sizeof(fs_t));
    fs->fs_name = HHSTR(name, name_len, 0);
    return fs;
}

void fs_register(fs_t* fs) {
    hstr_rehash(&fs->fs_name);
    list_append(&fs_list, &fs->fs_list);
}

fs_t* fs_find(const char* name) {
    hstr_t hname = HSTR(name, strlen(name));
    hstr_rehash(&hname);
    fs_t* pos, * n;
    list_for_each(pos, n, &fs_list, fs_list) {
        if (HSTR_EQ(&pos->fs_name, &hname)) {
            return pos;
        }
    }
    return NULL;
}